# -*- coding: utf-8 -*-

import pyglet
import state
import destroyer
import math

class Tile():
    
    def __init__(self, image, batch):
        self.sprite = pyglet.sprite.Sprite(image, batch=batch)
    
    def set_position(self, x, y):
        self.sprite.set_position(x, y)
    
    def set_opacity(self, n):
        self.sprite.opacity = n
        
class PlayState(state.State):

    def __init__(self):
        pass

    def __del__(self):
        pass

    def init(self):
        

        pyglet.gl.glClearColor(0, 0,10, 1)

        self.image_destroyer = pyglet.image.load('images/destroyer.png')
        self.destroyer = destroyer.Destroyer(image=self.image_destroyer)
        self.destroyer.setPosition(400, 300)
        self.destroyer.set_color(255, 0, 0)
        self.destroyer.set_color(255, 255, 255)
        
        self.image_block = pyglet.image.load('images/block8x8_1px_border.png')
        
        self.block_batch = pyglet.graphics.Batch()
        self.block_list=[]
        
        for x in range(100):
            for y in range(75):
                self.block_list.append(Tile(self.image_block, self.block_batch))
                self.block_list[len(self.block_list)-1].set_position(x*8, y*8)
                self.block_list[len(self.block_list)-1].set_opacity(128)
        '''self.block_list =[]
        for x in range(10):
            for y in range(10):
                self.block_list.append("1")
                print self.block_list[x][y]
                #self.block_list.append(pyglet.sprite.Sprite(self.image_block))
                #print self.block_list[x][y]
                print len(self.block_list)'''
        


        '''self.i_tank = pyglet.image.load('images/tank32_24.png')
        self.i_tank.anchor_x = self.i_tank.width//2
        self.i_tank.anchor_y = self.i_tank.height//2
        self.s_tank = pyglet.sprite.Sprite(self.i_tank)
        self.s_tank.set_position(400, 300)

        self.i_turret = pyglet.image.load('images/turret32x16.png')
        self.i_turret.anchor_x = 7
        self.i_turret.anchor_y = 8
        self.s_turret = pyglet.sprite.Sprite(self.i_turret)
        self.s_turret.set_position(400, 300)

        self.i_infantry = pyglet.image.load('images/infantry16x28.png')
        self.i_infantry_m = pyglet.image.load('images/m_infantry16x28.png')
        self.infantry = infantry.Infantry(image=self.i_infantry,
                                          image_marked=self.i_infantry_m)
        self.infantry.setPosition(400, 300)
        self.infantry.set_color(255, 0, 0)'''

    def draw(self, win):
        win.clear()
        '''self.s_tank.draw()
        self.s_turret.draw()'''
        self.block_batch.draw()
        #self.infantry.draw()
        self.destroyer.draw()

        win.fps_display.draw()


    def key_press(self, win, symbol, modifiers):

        if symbol == pyglet.window.key.ESCAPE:
            win.close()
            
        if symbol == pyglet.window.key.F:
            win.set_fullscreen(True)

        if symbol == pyglet.window.key.A:
            #self.infantry.rotate = -1
            pass

        if symbol == pyglet.window.key.D:
            #self.infantry.rotate = 1
            pass

        if symbol == pyglet.window.key.W:
            #self.infantry.setSpeed(15)
            pass

    def key_release(self, win, symbol, modifiers):

        if symbol == pyglet.window.key.A:
            #if self.infantry.rotate == -1:
                #self.infantry.rotate = 0
            pass

        if symbol == pyglet.window.key.D:
            #if self.infantry.rotate == 1:
                #self.infantry.rotate = 0
            pass

        if symbol == pyglet.window.key.S:
            #self.infantry.setSpeed(0)
            pass

    def mouse_motion(self, win, x, y, dx, dy):
        #self.s_turret.rotation = math.degrees(math.atan2(300 - y, x - 400))
        pass

    def mouse_press(self, win, x, y, button, modifiers):

        #if self.collision_sprite_point(self.infantry, x, y):
            '''if (not self.infantry.marked):
                self.infantry.mark()
            else:
                self.infantry.unmark()

        if self.infantry.marked:
            self.infantry.moveTo(x, y)'''
            pass


    def update(self, dt):
        '''self.infantry.update(dt)
        self.collision_sprite_point(self.infantry)'''
        pass

    def collision_sprite_point(self, sprite, p_x=0, p_y=0):

        x, y = sprite.getPosition()
        dx = x - p_x
        dy = y - p_y

        dist = math.sqrt(dx*dx + dy*dy)

        if (dist <= sprite.sprite.width/2):
            return True

        return False